
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, 2012 and Azure
-- --------------------------------------------------
-- Date Created: 04/26/2014 12:42:15
-- Generated from EDMX file: C:\Users\RazP\Google Drive\Projects\MVCKursProjekt\DAL\BlogModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [MVCProjectBlog];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_UserBlog]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Blog] DROP CONSTRAINT [FK_UserBlog];
GO
IF OBJECT_ID(N'[dbo].[FK_BlogBlogRoll]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BlogRoll] DROP CONSTRAINT [FK_BlogBlogRoll];
GO
IF OBJECT_ID(N'[dbo].[FK_BlogPostBlogComment]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BlogComment] DROP CONSTRAINT [FK_BlogPostBlogComment];
GO
IF OBJECT_ID(N'[dbo].[FK_BlogBlogPost]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BlogPost] DROP CONSTRAINT [FK_BlogBlogPost];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[User]', 'U') IS NOT NULL
    DROP TABLE [dbo].[User];
GO
IF OBJECT_ID(N'[dbo].[Blog]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Blog];
GO
IF OBJECT_ID(N'[dbo].[BlogPost]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BlogPost];
GO
IF OBJECT_ID(N'[dbo].[BlogRoll]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BlogRoll];
GO
IF OBJECT_ID(N'[dbo].[BlogComment]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BlogComment];
GO
IF OBJECT_ID(N'[dbo].[ErrorCatching]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ErrorCatching];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'User'
CREATE TABLE [dbo].[User] (
    [UserId] int IDENTITY(1,1) NOT NULL,
    [Created] datetime  NOT NULL,
    [Deleted] bit  NOT NULL,
    [Email] nvarchar(max)  NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Country] nvarchar(max)  NOT NULL,
    [Password] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Blog'
CREATE TABLE [dbo].[Blog] (
    [BlogId] int IDENTITY(1,1) NOT NULL,
    [Created] datetime  NOT NULL,
    [Deleted] bit  NOT NULL,
    [BlogName] nvarchar(max)  NOT NULL,
    [Title] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NOT NULL,
    [Owner_UserId] int  NOT NULL
);
GO

-- Creating table 'BlogPost'
CREATE TABLE [dbo].[BlogPost] (
    [BlogPostId] int IDENTITY(1,1) NOT NULL,
    [Created] datetime  NOT NULL,
    [Deleted] bit  NOT NULL,
    [Title] nvarchar(max)  NOT NULL,
    [PostText] nvarchar(max)  NOT NULL,
    [BlogPostedIn_BlogId] int  NOT NULL
);
GO

-- Creating table 'BlogRoll'
CREATE TABLE [dbo].[BlogRoll] (
    [BlogRollId] int IDENTITY(1,1) NOT NULL,
    [Deleted] bit  NOT NULL,
    [Link] nvarchar(max)  NOT NULL,
    [Blog_BlogId] int  NOT NULL
);
GO

-- Creating table 'BlogComment'
CREATE TABLE [dbo].[BlogComment] (
    [BlogCommentId] int IDENTITY(1,1) NOT NULL,
    [Created] datetime  NOT NULL,
    [Deleted] bit  NOT NULL,
    [Email] nvarchar(max)  NULL,
    [Name] nvarchar(max)  NULL,
    [Comment] nvarchar(max)  NOT NULL,
    [BlogPostCommentedOn_BlogPostId] int  NOT NULL
);
GO

-- Creating table 'ErrorCatching'
CREATE TABLE [dbo].[ErrorCatching] (
    [ErrorId] int IDENTITY(1,1) NOT NULL,
    [LoggedDate] datetime  NOT NULL,
    [ErrorMessage] nvarchar(max)  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [UserId] in table 'User'
ALTER TABLE [dbo].[User]
ADD CONSTRAINT [PK_User]
    PRIMARY KEY CLUSTERED ([UserId] ASC);
GO

-- Creating primary key on [BlogId] in table 'Blog'
ALTER TABLE [dbo].[Blog]
ADD CONSTRAINT [PK_Blog]
    PRIMARY KEY CLUSTERED ([BlogId] ASC);
GO

-- Creating primary key on [BlogPostId] in table 'BlogPost'
ALTER TABLE [dbo].[BlogPost]
ADD CONSTRAINT [PK_BlogPost]
    PRIMARY KEY CLUSTERED ([BlogPostId] ASC);
GO

-- Creating primary key on [BlogRollId] in table 'BlogRoll'
ALTER TABLE [dbo].[BlogRoll]
ADD CONSTRAINT [PK_BlogRoll]
    PRIMARY KEY CLUSTERED ([BlogRollId] ASC);
GO

-- Creating primary key on [BlogCommentId] in table 'BlogComment'
ALTER TABLE [dbo].[BlogComment]
ADD CONSTRAINT [PK_BlogComment]
    PRIMARY KEY CLUSTERED ([BlogCommentId] ASC);
GO

-- Creating primary key on [ErrorId] in table 'ErrorCatching'
ALTER TABLE [dbo].[ErrorCatching]
ADD CONSTRAINT [PK_ErrorCatching]
    PRIMARY KEY CLUSTERED ([ErrorId] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [Owner_UserId] in table 'Blog'
ALTER TABLE [dbo].[Blog]
ADD CONSTRAINT [FK_UserBlog]
    FOREIGN KEY ([Owner_UserId])
    REFERENCES [dbo].[User]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserBlog'
CREATE INDEX [IX_FK_UserBlog]
ON [dbo].[Blog]
    ([Owner_UserId]);
GO

-- Creating foreign key on [Blog_BlogId] in table 'BlogRoll'
ALTER TABLE [dbo].[BlogRoll]
ADD CONSTRAINT [FK_BlogBlogRoll]
    FOREIGN KEY ([Blog_BlogId])
    REFERENCES [dbo].[Blog]
        ([BlogId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_BlogBlogRoll'
CREATE INDEX [IX_FK_BlogBlogRoll]
ON [dbo].[BlogRoll]
    ([Blog_BlogId]);
GO

-- Creating foreign key on [BlogPostCommentedOn_BlogPostId] in table 'BlogComment'
ALTER TABLE [dbo].[BlogComment]
ADD CONSTRAINT [FK_BlogPostBlogComment]
    FOREIGN KEY ([BlogPostCommentedOn_BlogPostId])
    REFERENCES [dbo].[BlogPost]
        ([BlogPostId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_BlogPostBlogComment'
CREATE INDEX [IX_FK_BlogPostBlogComment]
ON [dbo].[BlogComment]
    ([BlogPostCommentedOn_BlogPostId]);
GO

-- Creating foreign key on [BlogPostedIn_BlogId] in table 'BlogPost'
ALTER TABLE [dbo].[BlogPost]
ADD CONSTRAINT [FK_BlogBlogPost]
    FOREIGN KEY ([BlogPostedIn_BlogId])
    REFERENCES [dbo].[Blog]
        ([BlogId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_BlogBlogPost'
CREATE INDEX [IX_FK_BlogBlogPost]
ON [dbo].[BlogPost]
    ([BlogPostedIn_BlogId]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------